﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Frame.Lib.Core.User;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Frame.Lib.Core.Config;
using Frame.Lib.Core.Register;
using Frame.Lib.Core.Log;

namespace Frame.Web
{
    public partial class Login : System.Web.UI.Page
    {
        private UserBO Users
        {
            get
            {
                return UserBO.Instance<UserBO>();
            }
        }
        private string strFirstModuleCode;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    //user.ClearSession();
                    string strLoginName = Request.QueryString["LoginName"];
                    strFirstModuleCode = Request.QueryString["ModuleCode"];
                    if (strLoginName != null)
                    {
                        string strPassword = Request["Password"];
                        strPassword = StrUtil.DecryptDES(strPassword);
                        CheckLogin(strLoginName, strPassword);
                    }

                    txtLoginName.Text = AppCookie.LoginName;
                }
            }
            catch (Exception ex)
            {
                JSUtil.Alert(ex.Message);
            }
        }
        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {

            string strLoginName = txtLoginName.Text.Trim();
            string strPassword = txtPassword.Text.Trim();


            CheckLogin(strLoginName, strPassword);

        }

        private void CheckLogin(string strLoginName, string strPassword)
        {
            bool expire = false;
            SysConfigBO config = new SysConfigBO();
            SysConfigInfo cfgInfo = config.GetInfo(ConfigID.reginfo.ToString());

            if (cfgInfo == null)
            {
                expire = true;
            }
            else
            {
                RegisterInfo info = (RegisterInfo)Serialize.BinaryDeserialize(cfgInfo.Config);
                expire = (info.ExpireDate < DateTime.Now);
            }

            if (expire)
            {
                JSUtil.Alert("error code 001");
                return;
            }

            LoginResult result = Users.CheckLogin(strLoginName, strPassword);

            bool blnFlag = true;
            switch (result)
            {
                case LoginResult.Forbidden:
                case LoginResult.PwdErr:
                case LoginResult.UserErr:
                    blnFlag = false;
                    break;
            }

            if (blnFlag)
            {
                PageBase.SetSession(strLoginName);
            }
            else
            {
                //AppSession.LoginName = strLoginName;//为写日志用的
                JSUtil.Alert("用户名或密码错！");
            }

            int userID = 0;
            if (!string.IsNullOrEmpty(AppSession.LoginName))
            {
                userID = AppSession.UserID;
            }

            Logger.Login(userID, strLoginName, AppSession.UserName, Request.UserHostAddress, (int)result);
            //  LogBO<LoginLogInfo>.LoginLog(result);//记录日志
            //blnFlag = true;
            if (blnFlag)
            {
                // LogMessageBO.Write("login", "test");
                string strUrl = "common/frame/default.aspx";
                if (strFirstModuleCode != null)
                {
                    strUrl += "?ModuleCode=" + strFirstModuleCode;
                }
                Response.Redirect(strUrl);
            }
        }
    }
}